package edu.sctu;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DataBase {
    public List<User> getUserList() {
        List<User> userList = new ArrayList<User>();
        //3+1
        try {


            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;


            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=?",
                    "root",
                    "123456"

            );

            String sql = "SELECT * FROM sctu_javaweb_2018";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String password = resultSet.getString(3);

                //封装
                User user = new User();
                user.setId(id);
                user.setName(username);
                user.setPassword(password);

                userList.add(user);


            }

            connection.close();
            statement.close();
            resultSet.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;

    }
}
